<?php
/********************************************************
** Copyright 2010-2013. Todos los derechos reservados. **
** Desarrollado y Gestionado por:					   **
** Sugey Quintero (Analista)						   **
** José G. De Freitas (Programador)					   **
** Klaudia Uribe (Diseñadora Web)					   **
** Cesar Isturiz (Diseñador Web)					   **
**				S E A  v1.0							   **
*********************************************************/

if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 15;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"conexion2.php";

date_default_timezone_set('America/Caracas'); setlocale(LC_TIME, 'spanish');

if ($_POST['Fecha']==''){
	$Fecha=date("Y-m-d");
}else{
	$Fecha=$_POST['Fecha'];
	$Fecha2=$_POST['Fecha2'];
}

if ($_POST['Usuario']==''){
	if($Fecha2){
		$query_pag_data = "SELECT Usuario, date_format(Fecha, '%d-%m-%Y %r'), Proceso FROM auditoria WHERE Fecha BETWEEN '$Fecha%' AND '$Fecha2%' ORDER BY Fecha Asc LIMIT $start, $per_page";
		$query_pag_num = "SELECT COUNT(*) AS count FROM auditoria WHERE Fecha BETWEEN '$Fecha%' AND '$Fecha2%' ";
	}else {
		$query_pag_data = "SELECT Usuario, date_format(Fecha, '%d-%m-%Y %r'), Proceso FROM auditoria WHERE Fecha LIKE '%$Fecha%' ORDER BY Fecha Asc LIMIT $start, $per_page";
		$query_pag_num = "SELECT COUNT(*) AS count FROM auditoria WHERE Fecha LIKE '%$Fecha%'";
	}
}else{
	$Usuario=$_POST['Usuario'];
	if($Fecha2){
		$query_pag_data = "SELECT Usuario, date_format(Fecha, '%d-%m-%Y %r'), Proceso FROM auditoria WHERE Usuario LIKE '%$Usuario%' AND Fecha BETWEEN '$Fecha%' AND '$Fecha2%' ORDER BY Fecha Asc LIMIT $start, $per_page";
		$query_pag_num = "SELECT COUNT(*) AS count FROM auditoria WHERE Usuario LIKE '%$Usuario%' AND Fecha BETWEEN '$Fecha%' AND '$Fecha2%' ";
	}else {
		$query_pag_data = "SELECT Usuario, date_format(Fecha, '%d-%m-%Y %r'), Proceso FROM auditoria WHERE Usuario LIKE '%$Usuario%' ORDER BY Fecha Asc LIMIT $start, $per_page";
		$query_pag_num = "SELECT COUNT(*) AS count FROM auditoria WHERE Usuario LIKE '%$Usuario%' ";
	}
}

$result_pag_data = mysql_query($query_pag_data);

$titulo= "<tr><td><br></td></tr><th width=20%>Usuario</th><th width=20%>Fecha y hora</th><th width=30%>Proceso</th>";
while ($row = mysql_fetch_array($result_pag_data)) {
    $msg .= "<tr><td align=center>".$row[0]."</td><td align=center>".$row[1]."</td><td align=center>".$row[2]."</td></tr>";
}
$msg = "<table class='data' border=0 cellspacing=5 width=100%><tr>".$titulo."</tr><tr>".$msg."</tr></table>"; // Content for Data


/* --------------------------------------------- */
$result_pag_num = mysql_query($query_pag_num);
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);

/* ---------------Calculating the starting and endign values for the loop----------------------------------- */
if ($cur_page >= 7) {
    $start_loop = $cur_page - 3;
    if ($no_of_paginations > $cur_page + 3)
        $end_loop = $cur_page + 3;
    else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
        $start_loop = $no_of_paginations - 6;
        $end_loop = $no_of_paginations;
    } else {
        $end_loop = $no_of_paginations;
    }
} else {
    $start_loop = 1;
    if ($no_of_paginations > 7)
        $end_loop = 7;
    else
        $end_loop = $no_of_paginations;
}
/* ----------------------------------------------------------------------------------------------------------- */
$msg .= "<div class='pagination'><ul>";

// FOR ENABLING THE FIRST BUTTON
if ($first_btn && $cur_page > 1) {
    $msg .= "<li p='1' class='active'>Primero</li>";
} else if ($first_btn) {
    $msg .= "<li p='1' class='inactive'>Primero</li>";
}

// FOR ENABLING THE PREVIOUS BUTTON
if ($previous_btn && $cur_page > 1) {
    $pre = $cur_page - 1;
    $msg .= "<li p='$pre' class='active'>Anterior</li>";
} else if ($previous_btn) {
    $msg .= "<li class='inactive'>Anterior</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {

    if ($cur_page == $i)
        $msg .= "<li p='$i' style='color:#fff;background-color:#6C7548;' class='active'>{$i}</li>";
    else
        $msg .= "<li p='$i' class='active'>{$i}</li>";
}

// TO ENABLE THE NEXT BUTTON
if ($next_btn && $cur_page < $no_of_paginations) {
    $nex = $cur_page + 1;
    $msg .= "<li p='$nex' class='active'>Siguiente</li>";
} else if ($next_btn) {
    $msg .= "<li class='inactive'>Siguiente</li>";
}

// TO ENABLE THE END BUTTON
if ($last_btn && $cur_page < $no_of_paginations) {
    $msg .= "<li p='$no_of_paginations' class='active'>Ultimo</li>";
} else if ($last_btn) {
    $msg .= "<li p='$no_of_paginations' class='inactive'>Ultimo</li>";
}
$msg = $msg . "</ul>" . $goto . $total_string . "</div>";  // Content for pagination
echo $msg;
}

